import { getUserCarList, addOrUpdateAmount, deleteUserCar } from "@/service/api/member.js"
import { AddCar, GetCarList, DeleteCar } from "@/service/api/user.js"
import {
    GetRefundOrderTempList,
    DeleteRefundOrderTemp,
    AddRefundOrderTemp,
    UpdateRefundOrderTemp,
    ChangeRefundOrderTempNumber,
    AddOrUpdateRefundOrderTemp,
} from "@/service/api/exChange.js"

import store from "@/store/index.js"
const valetCartUtil = {
    //获取总数
    getTempCartTotal(data, callBack) {
        let total = {
            totalNum: 0,
            totalPrice: 0,
            class: 0,
        }
        if (data.length > 0) {
            data.forEach((e) => {
                e.SubItems.forEach((item) => {
                    total.class += 1
                    total.totalNum += item.Amount
                    total.totalPrice += item.TotalMoney
                })
            })
        }
        total.totalPrice = total.totalPrice.toFixed(2)
        callBack(total)
    },

    //获取购物车
    getCarList: (valetUserId, warehouseId, isGift) => {
        return new Promise((resolve, reject) => {
            let param = {
                priceType: 1,
                isTempCar: true,
                valetUserId: valetUserId,
                warehouseId: warehouseId,
                isGift: isGift,
            }
            GetCarList(param).then((res) => {
                let items = []
                if (res && res.Data && res.Data.length > 0) {
                    res.Data.forEach((e) => {
                        let filterItem = items.filter((item) => {
                            return item.ProductId == e.ProductId
                        })
                        if (filterItem.length < 1) {
                            let SubItems = []
                            SubItems.push(e)
                            let obj = {
                                ProductId: e.ProductId,
                                ProductName: e.V_Title,
                                ProductImage: e.V_ThumbnailUrl160,
                                IsGift: e.IsGift,
                                SubItems: SubItems,
                            }
                            items.push(obj)
                        } else {
                            filterItem[0].SubItems.push(e)
                        }
                    })
                }
                store.commit("SET_VALET_CART", items)
                console.log(465)
                console.log(items)
                resolve(items)
            })
        })
    },

    //删除、清空购物车
    deleteRefundOrderTemp: (param) => {
        return new Promise((resolve, reject) => {
            let path = "?ids=" + param.ids + "&storeUserId=" + param.storeUserId + "&warehouseId=" + param.warehouseId
            DeleteCar(param, path).then((res) => {
                if (res.State) {
                    valetCartUtil.getCarList(param.storeUserId).then((res) => {
                        resolve(true)
                    })
                }
            })
        })
    },
    checkProIsInCart(item) {
        let result = null
        let datas = store.state.tempCartData
        datas.forEach((e) => {
            if (e.ProductId == item.ProductId) {
                e.SubItems.forEach((sub) => {
                    if (sub.UnitId == item.UnitId && sub.SkuId == item.SkuId) {
                        result = sub
                    }
                })
            }
        })
        return result
    },
    getAmountInTempCart(item) {
        let amount = 0
        let datas = store.state.tempCartData
        datas.forEach((e) => {
            if (e.ProductId == item.ProductId) {
                e.SubItems.forEach((sub) => {
                    if (sub.UnitId == item.UnitId && sub.SkuId == item.SkuId) {
                        amount = sub.Number
                    }
                })
            }
        })
        return amount
    },
    upValetCartFn(param) {
        //先判断是购物车里是已经有该商品
        return new Promise((resolve, reject) => {
            console.log("先判断是购物车里是已经有该商品")
            AddCar(param).then((res) => {
                if (res.State) {
                    let id = param.ValetUserId ? param.ValetUserId : param.valetUserId
                    let warehouseId = param.warehouseId
                    let isGift = param.isGift == true ? 1 : 0
                    console.log(isGift)
                    valetCartUtil.getCarList(id, warehouseId, isGift).then((res) => {
                        resolve(true)
                    })
                }
            })
        })
    },
}
module.exports = {
    getTempCartTotal: valetCartUtil.getTempCartTotal,
    getCartTotalForSelect: valetCartUtil.getCartTotalForSelect,
    updateCart: valetCartUtil.updateCart,
    deleteRefundOrderTemp: valetCartUtil.deleteRefundOrderTemp,
    getCarList: valetCartUtil.getCarList,
    upValetCartFn: valetCartUtil.upValetCartFn,
    getAmountInTempCart: valetCartUtil.getAmountInTempCart,
}
